﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestConsole;

namespace TestConsole
{
    public class JsGen
    {
        //var male = document.getElementById("male");
        //        male.checked = true;
        //// checkbox的勾选
        //var male = document.getElementById("sportId");
        //        male.checked = true;
        //// select单选下拉框
        //var city = document.getElementById("selectId");
        //        city.value = 'cp';
        //// 文本框输入
        //var city = document.getElementById("textId");
        //        city.value = 'text';
        public string GetJsByFile(string fileName)
        {
            InfoSaver saver = new InfoSaver();
            var controls = saver.Get<List<FormControlBase>>(fileName);
            if (controls != null)
            {
                return GetJs(controls);
            }
            return string.Empty;
        }
        public string GetJs(List<FormControlBase> controls)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in controls)
            {
                if (item.GType == GType.none)
                {
                    if (item is InputInfo input)
                    {
                        // date 和 text类型的input 走这个分支
                        var name = getNewName();
                        sb.AppendLine($"var {name}=document.getElementById(\"{input.Id}\");");
                        sb.AppendLine($"{name}.value='{input.StringValue}'");
                    }
                    else if (item is TextareaInfo area)
                    {
                        var name = getNewName();
                        sb.AppendLine($"var {name}=document.getElementById(\"{area.Id}\");");
                        sb.AppendLine($"{name}.value='{area.StringValue}'");
                    }
                    else if (item is SelectInfo select)
                    {
                        var name = getNewName();
                        sb.AppendLine($"var {name}=document.getElementById(\"{select.Id}\");");
                        sb.AppendLine($"{name}.value='{select.StringValue}'");
                    }
                }
                else if (item.GType == GType.checkbox || item.GType == GType.radio)
                {
                    var parentGroup = item as InputInfo;
                    foreach (var child in item.Children)
                    {
                        var input = child as InputInfo;
                        if (parentGroup.StringValue.Contains(input.Label))
                        {
                            var name = getNewName();
                            sb.AppendLine($"var {name}=document.getElementById(\"{child.Id}\");");
                            sb.AppendLine($"{name}.checked='true'");
                        }
                    }
                }
                else
                {
                    throw new Exception($"未处理{item.GType}类型的控件");
                }
            }
            return sb.ToString();
        }

        int nameIndex = 1;
        private string getNewName()
        {
            return $"name_{nameIndex++}";
        }
    }
}
